<?php
/**
 * Login Class
 * @package mobi.application.modules.admin
 * @author xoannt
 */
class Login extends CI_Controller {
/**
 * construct
 * 
 * @access public
 */
    public function __construct()
    {
        parent::__construct();  
        $this->load->model('user_model');
        $this->load->library("check_form");
           
    }
/**
 * user login
 * 
 * @access public
 */
    public function index()
    {
           
        $this->form_validation->set_rules('txtname', 'Username', 'required');
        $this->form_validation->set_rules('txtpass', 'Password', 'required|md5|callback_checkdb');   
        $this->form_validation->set_message('required', '%s  is not empty');
        $this->form_validation->set_error_delimiters('<i style="color:red">','</i>');
        if($this->form_validation->run()=== FALSE)
        {
            $data['baseURL'] = base_url();
            $this->load->view('login/index',$data);
        }
        else
        {
            redirect('admin');      
        }
    }
/**
 * check input form
 * 
 * @access public
 * @return boolean 
 */
    public function checkdb()
    {
        if($this->input->post())
        {
            $username_input = $this->input->post('txtname'); $password_input = $this->input->post('txtpass');
            $username = $this->check_form->check_sql_one($username_input); $password = $this->check_form->check_sql_one($password_input);
            $result = $this->user_model->login($username,$password);
            if($result)
	    {
                foreach($result as $row)
                {
                    $username = $row->user_name;
                }
	        $this->session->set_userdata('username', $username);
                return TRUE;
	    }
            else
            {
                $this->form_validation->set_message('checkdb','Invalid username|password');
                return FALSE;
            }  
        }
    }  
}